﻿using AutoMapper;
using D.B.Data.API.Applications.Command.MenusCommand;
using D.B.Data.Domain;
using D.B.Data.Domain.DTO.Output;
using D.B.Data.ErrorCode;
using D.B.Data.Infrastructuse;
using MediatR;
using Microsoft.EntityFrameworkCore;
using System.Security.Cryptography;

namespace D.B.Data.API.Applications.CommandHandler.MenusCommandHandler
{
    public class QueryMenusCommandHandler : IRequestHandler<QueryMenusCommand, APIPaging<QueryMenusDTO>>
    {
        private readonly IBaseRepository<MenusModel> baseRepository;
        private readonly IBaseRepository<UserModel> userRepository;
        private readonly IMapper mapper;


        public QueryMenusCommandHandler(IBaseRepository<MenusModel> baseRepository, IMapper mapper, IBaseRepository<UserModel> userRepository)
        {
            this.baseRepository = baseRepository;
            this.mapper = mapper;
            this.userRepository = userRepository;
        }

        public Task<APIPaging<QueryMenusDTO>>Handle(QueryMenusCommand request, CancellationToken cancellationToken)
        {

            var menuQuery = baseRepository.GetAll().AsNoTracking().Where(m => !m.IsDel);
            var userQuery = userRepository.GetAll().AsNoTracking().Where(u => !u.IsDel);
           
                       
                        
                        

            var data = from a in menuQuery
                       join b in menuQuery.DefaultIfEmpty() // 左连接，处理顶级菜单
                       on a.Pid equals b.Id
                       into parentGroup
                       from b in parentGroup.DefaultIfEmpty() // 确保左连接语义

                       join c in userQuery
                       on a.CreateUserId equals c.Id

                       join d in userQuery on a.UpDataUserId equals d.Id into updaters
                       from d in updaters.DefaultIfEmpty()
                       where !a.IsDel && (!b.IsDel|| b==null) && !c.IsDel
                       select new QueryMenusDTO
                       {
                           Url = a.Url,
                           CreateTime = a.CreateTime,
                           CreateUserName = c.UserName,
                           UpDataTime = a.UpDataTime,
                           UPUserName =d.UserName==null?"": d.UserName, // 防止空引用异常
                           Id = a.Id,
                           MenuName = a.MenuName,
                           ParentName =b.MenuName==null? "顶级菜单" : b.MenuName, // 防止空引用异常
                            Pid= a.Pid,
                             CreateUserId=a.CreateUserId,
                             UpDataUserId=a.UpDataUserId,
                           
                       };

            var count = data.Count();
            var res= data.OrderByDescending(s=>s.Id).Skip((request.PageIndex-1)*request.PageSize).Take(request.PageSize).ToList();

            APIPaging<QueryMenusDTO> aPIResult = new APIPaging<QueryMenusDTO>()
            {
                Code = EnumCode.Ok,
                Msg = "成功",
                PageData= res,
                Count=count,
            };

            return Task.FromResult(aPIResult);
        }
    }
}
